//
//  MonthButton.swift
//  StoreProject
//
//  Created by CYC on 2021/9/28.
//

import UIKit

class MonthButton: UIButton {

    var model = MonthModel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("")
    }
    
    
    // MARK:创建UI
    func creatUIAction(model: MonthModel) {
        
        self.model = model
        self.removeAllSubviews()
        
        let monthLab = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: self.c_width(), height: 20))
        monthLab.textColor = kLabelColor1
        monthLab.textAlignment = .left
        monthLab.font = kBFont(16)
        monthLab.text = String.init(format: "%d月", model.month)
        self.addSubview(monthLab)
        
        // 确定这个月第一天是星期几
        let firstDay = model.dayList.first
        let firstIndex = (firstDay?.date.weekday)! - 1
        
        let HN = 7      // 水平方向个数
        let VN = 6      // 垂直方向个数
        let BW = self.c_width() / CGFloat(HN)                           // 单元宽度
        let BH = (self.c_height() - monthLab.c_bottom()) / CGFloat(VN)      // 单元高度
        for index in 0..<HN*VN {
            
            let modelIndex = index - firstIndex
            
            if modelIndex < model.dayList.count && modelIndex >= 0 {
                
                let dayModel = model.dayList[modelIndex]
                
                let view1 = UIView.init(frame: CGRect.init(x: BW * CGFloat(index % HN), y: monthLab.c_bottom() + BH * CGFloat(index/HN), width: BW, height: BH))
                view1.backgroundColor = .clear
                view1.isUserInteractionEnabled = false
                self.addSubview(view1)
                
                let text = String.init(format: "%d", dayModel.day)
                let lab = UILabel.init(frame: view1.bounds)
                lab.textColor = kLabelColor1
                lab.textAlignment = .center
                lab.font = kMFont(10)
                lab.text = text
                view1.addSubview(lab)
                
                
                
                
                // 今天
                if dayModel.date.year == kTodayCom.year && dayModel.date.month == kTodayCom.month && dayModel.date.day == kTodayCom.day {

                    var size = BW
                    if BW > BH {
                        size = BH
                    }
                    let pointView = UIView.init(frame: .init(x: (view1.c_width() - size)/2.0, y: (view1.c_height() - size)/2.0, width: size, height: size))
                    pointView.c_radius(radius: size/2.0)
                    pointView.backgroundColor = kMainColor
                    view1.insertSubview(pointView, at: 0)
                    
                    lab.textColor = .white
                    
                } else {
                    
                    let size = text.boundingRect(with: .init(width: kScreenWidth, height: CGFloat(MAXFLOAT)),
                                                 options: .usesLineFragmentOrigin,
                                                 attributes: [NSAttributedString.Key.font : kMFont(10)],
                                                 context: nil);
                    
                    if dayModel.chineseDay.hasSuffix("正月初一") == true {
                        
                        // 大年初一
                        let line = UIView.init(frame: CGRect.init(x: (view1.c_width() - size.width)/2.0, y: view1.c_height() - kLineSize*2, width: size.width, height: kLineSize*2))
                        line.backgroundColor = kMainColor
                        view1.addSubview(line)
                    } else {
                        
                        // 农历初一
                        if dayModel.chineseSubDay == "初一" {
                            
                            let line = UIView.init(frame: CGRect.init(x: (view1.c_width() - size.width)/2.0, y: view1.c_height() - kLineSize, width: size.width, height: kLineSize))
                            line.backgroundColor = kMainColor
                            view1.addSubview(line)
                        }
                    }
                    
                }
            }
        }
        
        
    }

}
